package org.dromara.third.convert.trouble.web;

import com.alibaba.fastjson.JSONObject;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.dromara.common.core.domain.R;
import org.dromara.common.core.exception.BusinessServiceException;
import org.dromara.common.core.utils.DateUtils;
import org.dromara.common.core.utils.StringUtils;
import org.dromara.common.core.utils.ValidateUtil;
import org.dromara.system.domain.Trouble;
import org.dromara.system.mapper.TroubleMapper;
import org.dromara.third.auth.beans.constans.ExceptionEnum;
import org.dromara.third.auth.common.auth.TokenThreadLocal;
import org.dromara.third.auth.util.AESUtil;
import org.dromara.third.convert.trouble.dto.TroubleDTO;
import org.dromara.third.convert.trouble.validator.TroubleValidator;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

@RequiredArgsConstructor
@RestController
@Slf4j
@RequestMapping("/api/v1")
public class ConvertTroubleController {

    @Autowired
    private TroubleMapper troubleMapper;

    @PostMapping("/trouble")
    @Transactional
    public R<Void> addTrouble(@RequestBody String jsonReq) {
        String orgCode = TokenThreadLocal.getThreadLocal();
        TroubleDTO troubleDTO;
        try {
            String decrypt = AESUtil.decrypt(jsonReq);
            troubleDTO = JSONObject.parseObject(decrypt, TroubleDTO.class);
        } catch (Exception e) {
            log.info("解析参数异常");
            throw new BusinessServiceException(ExceptionEnum.AuthException06.getCode(), ExceptionEnum.AuthException06.getMessage());
        }
        // 校验参数
        List<String> errorMessageList = new ArrayList<>();
        ValidateUtil.validate(errorMessageList, troubleDTO, new TroubleValidator());
        if (!CollectionUtils.isEmpty(errorMessageList)) {
            String errorMessage = StringUtils.join(errorMessageList, ",");
            throw new BusinessServiceException(ExceptionEnum.AuthException06.getCode(), errorMessage);
        }
        Trouble trouble = assembleTrouble(orgCode, troubleDTO);
        int count = troubleMapper.countWithOutDelFlag(trouble.getId());
        if (count == 0) {
            troubleMapper.insert(trouble);
        } else {
            troubleMapper.updateWithOutDelFlag(trouble);
        }
        return R.ok();
    }

    private Trouble assembleTrouble(String orgCode, TroubleDTO dto) {
        Trouble trouble = new Trouble();
        try {
            trouble.setId(dto.getId());
            trouble.setDangerName(dto.getDangerName());
            trouble.setHazardCode(dto.getHazardCode());
            trouble.setRiskMeasureId(dto.getRiskMeasureId());
            trouble.setRecordId(dto.getRecordId());
            trouble.setDangerLevel(dto.getDangerLevel());
            trouble.setRegistTime(DateUtils.parseDate(dto.getRegistTime()));
            trouble.setRegistrant(dto.getRegistrant());
            trouble.setDangerSrc(dto.getDangerSrc());
            trouble.setEnforcementId(dto.getEnforcementId());
            trouble.setDangerManageType(dto.getDangerManageType());
            trouble.setHazardDangerType(dto.getHazardDangerType());
            trouble.setHazardCategory(dto.getHazardCategory());
            trouble.setDangerDesc(dto.getDangerDesc());
            trouble.setDangerReason(dto.getDangerReason());
            trouble.setControlMeasures(dto.getControlMeasures());
            trouble.setCost(dto.getCost());
            trouble.setLiablePerson(dto.getLiablePerson());
            trouble.setDangerManageDeadline(DateUtils.parseDate(dto.getDangerManageDeadline()));
            trouble.setCheckAcceptPerson(dto.getCheckAcceptPerson());
            trouble.setCheckAcceptTime(DateUtils.parseDate(dto.getCheckAcceptTime()));
            trouble.setCheckAcceptComment(dto.getCheckAcceptComment());
            trouble.setState(dto.getState());
            trouble.setAttachments1(dto.getAttachments1());
            trouble.setAttachments2(dto.getAttachments2());
            trouble.setDelFlag(Long.parseLong(dto.getDelFlag()));
            trouble.setCreateDept(Long.parseLong(dto.getCreateDept()));
            trouble.setCreateBy(Long.parseLong(dto.getCreateBy()));
            trouble.setCreateTime(DateUtils.parseDate(dto.getCreateTime()));
            trouble.setUpdateBy(Long.parseLong(dto.getUpdateBy()));
            trouble.setUpdateTime(DateUtils.parseDate(dto.getUpdateTime()));
            trouble.setTenantId(orgCode);
        } catch (Exception e) {
            throw new BusinessServiceException(ExceptionEnum.AuthException07.getCode(), "组装隐患信息异常");
        }
        return trouble;
    }
}
